package com.activemqwrap.tools.tcpforward;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
//import java.io.IOException;
//import java.net.InetAddress;
//import java.net.ServerSocket;
//import java.net.Socket;

/**
 * 主程序
 * 运行示例：
 * java -jar activemqwrap.jar 127.0.0.1 8081 192.168.20.11 22
 * 参数顺序说明：当前服务器IP地址、当前服务器端口、目的地服务器IP地址、目的地服务器端口
 */
public class App {

    public static void main(String[] args) {
        ProxyMQ proxyMQ = null;
        WatchDog watchDog = null;

        try {
            File f = new File("config.xml");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(f);
            NodeList nl = doc.getElementsByTagName("VALUE");

            String ipSource = doc.getElementsByTagName("ipSource").item(0).getFirstChild().getNodeValue();
            String portSource = doc.getElementsByTagName("portSource").item(0).getFirstChild().getNodeValue();
            String ipTarget = doc.getElementsByTagName("ipTarget").item(0).getFirstChild().getNodeValue();
            String portTarget = doc.getElementsByTagName("portTarget").item(0).getFirstChild().getNodeValue();
            String mqHost = doc.getElementsByTagName("mqHost").item(0).getFirstChild().getNodeValue();
            String userName = doc.getElementsByTagName("userName").item(0).getFirstChild().getNodeValue();
            String password = doc.getElementsByTagName("password").item(0).getFirstChild().getNodeValue();
            String[] queueNames = doc.getElementsByTagName("queueNames").item(0).getFirstChild().getNodeValue().split(",");
            String showDebug = doc.getElementsByTagName("showDebug").item(0).getFirstChild().getNodeValue();

            proxyMQ = new ProxyMQ();

            proxyMQ.socketArg = new String[]{ipSource, portSource, ipTarget, portTarget};
            proxyMQ.DEFAULT_BROKER_HOST = mqHost;
            proxyMQ.userName = userName;
            proxyMQ.password = password;
            proxyMQ.queueNames = queueNames;

            //proxyMQ.getAllQueueAndDelete();
            if (showDebug.equals("1"))
                proxyMQ.showDebug = true;
            proxyMQ.start(0);

            //放狗
            watchDog = new WatchDog(proxyMQ);
            watchDog.killThreshold = 5 * 60 * 1000;//15分钟
            watchDog.runDog();

        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            proxyMQ.stop();
            watchDog.stopDog();
        }

    }

    public void test001() {
        try {
            //            proxyMQ.showDebug = false;
//            proxyMQ.start(0);
//
////            proxyMQ.getAllQueueAndDelete();

//            PushPopActivMQ.sendMQ("");
//            PushPopActivMQ.rcvMQ("queueName");
//            PushPopActivMQ.borwserQueue();

//            SoketAndMQ sm = new SoketAndMQ();
//            sm.args = new String[]{"192.168.1.222", "9999", "192.168.1.230", "8081"};
//            sm.run(0);

//            PushPopActivMQ.getAllQueueAndDelete();


        } catch (Exception e) {
            String a = e.getMessage();
        }

    }

}
